package scoreAnalyze;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CourseMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    static Text courseName1 = new Text();
    static Text courseName2 = new Text();
    public void map(LongWritable k1, Text v1, Context context)
        throws IOException, InterruptedException{
        String[] splits = v1.toString().split(" ");
        if(splits.length != 4){
            System.err.println("Error");
            return;
        }

        if(v1.toString().contains("id")){
            courseName1.set(splits[2]);
            courseName2.set(splits[3]);
            System.out.println(courseName1.toString());
        }
        else{
            int score1 = Integer.parseInt(splits[2]);
            int score2 = Integer.parseInt(splits[3]);
            context.write(courseName1, new IntWritable(score1));
            context.write(courseName2, new IntWritable(score2));
        }
    }
}
